package com.lizard.schedule.ui.activity.adapter;

import android.content.Context;
import android.support.wearable.view.WearableListView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lizard.schedule.R;
import com.lizard.schedule.model.WearSchedule;
import com.lizard.schedule.ui.activity.view.ScheduleItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyiqian on 16/6/8.
 */

public class ScheduleListAdapter extends WearableListView.Adapter {

    private Context context;

    private List<WearSchedule> wearSchedules = new ArrayList<>();

    public ScheduleListAdapter(Context context){
        this.context = context;
    }

    public void setData(List<WearSchedule> wearSchedules){
        this.wearSchedules.clear();
        addData(wearSchedules);
    }

    public void addData(List<WearSchedule> wearSchedules){
        if(wearSchedules != null){
            this.wearSchedules.addAll(wearSchedules);
            notifyDataSetChanged();
        }
    }

    public void addData(WearSchedule wearSchedule){
        if(wearSchedules != null){
            List<WearSchedule> wearSchedules = new ArrayList<>();
            wearSchedules.add(wearSchedule);
            addData(wearSchedules);
        }
    }

    public void clearData(){
        wearSchedules.clear();
        notifyDataSetChanged();
    }

    public WearSchedule getItem(int pos){
        return wearSchedules.get(pos);
    }

    @Override
    public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(new ScheduleItemView(context));
    }

    @Override
    public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
        WearSchedule wearSchedule = wearSchedules.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        String timeTxt = (TextUtils.isEmpty(wearSchedule.getDayNearby()) ? wearSchedule.getDateStr() : wearSchedule.getDayNearby())
                + " " + wearSchedule.getTimeStr();
        viewHolder.time.setText(timeTxt);
        viewHolder.content.setText(wearSchedule.getContentText());
        viewHolder.finishedFlag.setVisibility(wearSchedule.getStatus() == WearSchedule.STATUS_FINISHED ? View.VISIBLE : View.GONE);
    }

    @Override
    public int getItemCount() {
        return wearSchedules.size();
    }

    private static class ViewHolder extends WearableListView.ViewHolder{

        TextView time;

        TextView content;

        View finishedFlag;

        public ViewHolder(View itemView){
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.time);
            content = (TextView) itemView.findViewById(R.id.content);
            finishedFlag = itemView.findViewById(R.id.finished_flag);
        }

    }
}
